<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Namespace Handling [Universal Feed Parser]</title>
<link rel="stylesheet" href="feedparser.css" type="text/css">
<link rev="made" href="mailto:mark@diveintomark.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
<meta name="keywords" content="RSS, Atom, CDF, XML, feed, parser, Python">
<link rel="start" href="index.html" title="Documentation">
<link rel="up" href="advanced.html" title="Advanced Features">
<link rel="prev" href="content-normalization.html" title="Content Normalization">
<link rel="next" href="resolving-relative-links.html" title="Relative Link Resolution">
</head>
<body id="feedparser-org" class="docs">
<div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
<div class="s" id="pageHeader">
<h1><a href="/"><span>Universal Feed Parser</span></a></h1>
<p><span>Parse RSS and Atom feeds in Python.  3000 unit tests.  Open source.</span></p>
</div>
<div class="s" id="quickSummary"><ul>
<li class="li1">
<a href="http://sourceforge.net/projects/feedparser/"><span>Download</span></a> ·</li>
<li class="li2">
<a href="http://feedparser.org/docs/"><span>Documentation</span></a> ·</li>
<li class="li3">
<a href="http://feedparser.org/tests/"><span>Unit tests</span></a> ·</li>
<li class="li4"><a href="http://sourceforge.net/tracker/?func=browse&amp;group_id=112328&amp;atid=661937"><span>Report a bug</span></a></li>
</ul></div>
</div></div></div>
<div id="main"><div id="mainInner">
<p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <a href="advanced.html">Advanced Features</a> → <span class="thispage">Namespace Handling</span></p>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div><h2 class="title">
<a name="advanced.namespaces" class="skip" href="#advanced.namespaces" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Namespace Handling</h2></div>
<div><div class="abstract">
<h3 class="title"></h3>
<p><span class="application">Universal Feed Parser</span> attempts to expose all possible data in feeds, including elements in extension namespaces.</p>
</div></div>
</div>
<div></div>
</div>
<p>Some common namespaced elements are mapped to core elements.  For further information about these mappings, see <a href="reference.html" title="Reference">Reference</a>.</p>
<p>Other namespaced elements are available as <tt class="varname"><i class="replaceable"><tt>prefix</tt></i>_<i class="replaceable"><tt>element</tt></i></tt>.</p>
<p>The namespaces defined in the feed are available in the parsed results as <tt class="varname">namespaces</tt>, a dictionary of <tt class="literal">{prefix: namespaceURI}</tt>.  If the feed defines a default namespace, it is listed as <tt class="varname">namespaces['']</tt>.</p>
<div class="example">
<a name="example.namespace" class="skip" href="#example.namespace" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Accessing namespaced elements</h3>
<pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><font color='navy'><b>import</b></font> feedparser</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d = feedparser.parse('<a href="http://feedparser.org/docs/examples/prism.rdf">http://feedparser.org/docs/examples/prism.rdf</a>')</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.prism_issn</span>
<span class="computeroutput">u'0028-0836'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.namespaces</span>
<span class="computeroutput">{'': u'http://purl.org/rss/1.0/',
 'prism': u'http://prismstandard.org/namespaces/1.2/basic/',
 'rdf': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}</span></pre>
</div>
<p>The prefix used to construct the variable name is not guaranteed to be the same as the prefix of the namespaced element in the original feed.  If <span class="application">Universal Feed Parser</span> recognizes the namespace, it will use the namespace's preferred prefix to construct the variable name.  It will also list the namespace in the <tt class="varname">namespaces</tt> dictionary using the namespace's preferred prefix.</p>
<p>In the previous example, the namespace (<tt class="literal">http://prismstandard.org/namespaces/1.2/basic/</tt>) was defined with the namespace's preferred prefix (<tt class="literal">prism</tt>), so the <tt class="sgmltag-element">prism:issn</tt> element was accessible as the variable <tt class="varname">d.feed.prism_issn</tt>.  However, if the namespace is defined with a non-standard prefix, <span class="application">Universal Feed Parser</span> will still construct the variable name using the preferred prefix, <span class="emphasis"><em>not</em></span> the actual prefix that is used in the feed.</p>
<p>This will become clear with an example.</p>
<div class="example">
<a name="example.namespace.nonstandard" class="skip" href="#example.namespace.nonstandard" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Accessing namespaced elements with non-standard prefixes</h3>
<pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><font color='navy'><b>import</b></font> feedparser</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d = feedparser.parse('<a href="http://feedparser.org/docs/examples/nonstandard_prefix.rdf">http://feedparser.org/docs/examples/nonstandard_prefix.rdf</a>')</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.prism_issn</span>
<span class="computeroutput">u'0028-0836'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.foo_issn</span>
<span class="traceback">Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
  File "feedparser.py", line 158, in __getattr__
    raise AttributeError, "object has no attribute '%s'" % key
AttributeError: object has no attribute 'foo_issn'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.namespaces</span>
<span class="computeroutput">{'': u'http://purl.org/rss/1.0/',
 'prism': u'http://prismstandard.org/namespaces/1.2/basic/',
 'rdf': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}</span></pre>
</div>
<p>This is the complete list of namespaces that <span class="application">Universal Feed Parser</span> recognizes and uses to construct the variable names for data in these namespaces:</p>
<div class="table">
<a name="id4957346" class="skip" href="#id4957346" title="link to this table"><img src="images/permalink.gif" alt="[link]" title="link to this table" width="8" height="9"></a> <h3 class="title">Recognized Namespaces</h3>
<table summary="Recognized Namespaces" border="1">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>Prefix</th>
<th>Namespace</th>
</tr></thead>
<tbody>
<tr>
<td><tt class="literal">admin</tt></td>
<td><tt class="literal">http://webns.net/mvcb/</tt></td>
</tr>
<tr>
<td><tt class="literal">ag</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/aggregation/</tt></td>
</tr>
<tr>
<td><tt class="literal">annotate</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/annotate/</tt></td>
</tr>
<tr>
<td><tt class="literal">audio</tt></td>
<td><tt class="literal">http://media.tangent.org/rss/1.0/</tt></td>
</tr>
<tr>
<td><tt class="literal">blogChannel</tt></td>
<td><tt class="literal">http://backend.userland.com/blogChannelModule</tt></td>
</tr>
<tr>
<td><tt class="literal">cc</tt></td>
<td><tt class="literal">http://web.resource.org/cc/</tt></td>
</tr>
<tr>
<td><tt class="literal">creativeCommons</tt></td>
<td><tt class="literal">http://backend.userland.com/creativeCommonsRssModule</tt></td>
</tr>
<tr>
<td><tt class="literal">co</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/company</tt></td>
</tr>
<tr>
<td><tt class="literal">content</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/content/</tt></td>
</tr>
<tr>
<td><tt class="literal">cp</tt></td>
<td><tt class="literal">http://my.theinfo.org/changed/1.0/rss/</tt></td>
</tr>
<tr>
<td><tt class="literal">dc</tt></td>
<td><tt class="literal">http://purl.org/dc/elements/1.1/</tt></td>
</tr>
<tr>
<td><tt class="literal">dcterms</tt></td>
<td><tt class="literal">http://purl.org/dc/terms/</tt></td>
</tr>
<tr>
<td><tt class="literal">email</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/email/</tt></td>
</tr>
<tr>
<td><tt class="literal">ev</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/event/</tt></td>
</tr>
<tr>
<td><tt class="literal">feedburner</tt></td>
<td><tt class="literal">http://rssnamespace.org/feedburner/ext/1.0</tt></td>
</tr>
<tr>
<td><tt class="literal">fm</tt></td>
<td><tt class="literal">http://freshmeat.net/rss/fm/</tt></td>
</tr>
<tr>
<td><tt class="literal">foaf</tt></td>
<td><tt class="literal">http://xmlns.com/foaf/0.1/</tt></td>
</tr>
<tr>
<td><tt class="literal">geo</tt></td>
<td><tt class="literal">http://www.w3.org/2003/01/geo/wgs84_pos#</tt></td>
</tr>
<tr>
<td><tt class="literal">icbm</tt></td>
<td><tt class="literal">http://postneo.com/icbm/</tt></td>
</tr>
<tr>
<td><tt class="literal">image</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/image/</tt></td>
</tr>
<tr>
<td><tt class="literal">itunes</tt></td>
<td><tt class="literal">http://www.itunes.com/DTDs/PodCast-1.0.dtd</tt></td>
</tr>
<tr>
<td><tt class="literal">itunes</tt></td>
<td><tt class="literal">http://example.com/DTDs/PodCast-1.0.dtd</tt></td>
</tr>
<tr>
<td><tt class="literal">l</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/link/</tt></td>
</tr>
<tr>
<td><tt class="literal">media</tt></td>
<td><tt class="literal">http://search.yahoo.com/mrss</tt></td>
</tr>
<tr>
<td><tt class="literal">pingback</tt></td>
<td><tt class="literal">http://madskills.com/public/xml/rss/module/pingback/</tt></td>
</tr>
<tr>
<td><tt class="literal">prism</tt></td>
<td><tt class="literal">http://prismstandard.org/namespaces/1.2/basic/</tt></td>
</tr>
<tr>
<td><tt class="literal">rdf</tt></td>
<td><tt class="literal">http://www.w3.org/1999/02/22-rdf-syntax-ns#</tt></td>
</tr>
<tr>
<td><tt class="literal">rdfs</tt></td>
<td><tt class="literal">http://www.w3.org/2000/01/rdf-schema#</tt></td>
</tr>
<tr>
<td><tt class="literal">ref</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/reference/</tt></td>
</tr>
<tr>
<td><tt class="literal">reqv</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/richequiv/</tt></td>
</tr>
<tr>
<td><tt class="literal">search</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/search/</tt></td>
</tr>
<tr>
<td><tt class="literal">slash</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/slash/</tt></td>
</tr>
<tr>
<td><tt class="literal">soap</tt></td>
<td><tt class="literal">http://schemas.xmlsoap.org/soap/envelope/</tt></td>
</tr>
<tr>
<td><tt class="literal">ss</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/servicestatus/</tt></td>
</tr>
<tr>
<td><tt class="literal">str</tt></td>
<td><tt class="literal">http://hacks.benhammersley.com/rss/streaming/</tt></td>
</tr>
<tr>
<td><tt class="literal">sub</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/subscription/</tt></td>
</tr>
<tr>
<td><tt class="literal">sy</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/syndication/</tt></td>
</tr>
<tr>
<td><tt class="literal">taxo</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/taxonomy/</tt></td>
</tr>
<tr>
<td><tt class="literal">thr</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/threading/</tt></td>
</tr>
<tr>
<td><tt class="literal">ti</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/textinput/</tt></td>
</tr>
<tr>
<td><tt class="literal">trackback</tt></td>
<td><tt class="literal">http://madskills.com/public/xml/rss/module/trackback/</tt></td>
</tr>
<tr>
<td><tt class="literal">wfw</tt></td>
<td><tt class="literal">http://wellformedweb.org/CommentAPI/</tt></td>
</tr>
<tr>
<td><tt class="literal">wiki</tt></td>
<td><tt class="literal">http://purl.org/rss/1.0/modules/wiki/</tt></td>
</tr>
<tr>
<td><tt class="literal">xhtml</tt></td>
<td><tt class="literal">http://www.w3.org/1999/xhtml</tt></td>
</tr>
<tr>
<td><tt class="literal">xml</tt></td>
<td><tt class="literal">http://www.w3.org/XML/1998/namespace</tt></td>
</tr>
</tbody>
</table>
</div>
<p></p>
<a name="id4958155"></a><table class="note" border="0" summary="">
<tr><td rowspan="2" align="center" valign="top" width="1%"><img src="images/note.png" alt="Note" title="" width="24" height="24"></td></tr>
<tr><td colspan="2" align="left" valign="top" width="99%">
<span class="application">Universal Feed Parser</span> treats namespaces as case-insensitive to match the behavior of certain versions of <span class="application">iTunes</span>.</td></tr>
</table>
<a name="id4958175"></a><table class="caution" border="0" summary="">
<tr><td rowspan="2" align="center" valign="top" width="1%"><img src="images/caution.png" alt="Caution" title="" width="24" height="24"></td></tr>
<tr><td colspan="2" align="left" valign="top" width="99%">Data from namespaced elements is not <a href="html-sanitization.html" title="HTML Sanitization">sanitized</a> (even if it contains <acronym title="HyperText Markup Language">HTML</acronym> markup).</td></tr>
</table>
</div>
<div style="float: left">← <a class="NavigationArrow" href="content-normalization.html">Content Normalization</a>
</div>
<div style="text-align: right">
<a class="NavigationArrow" href="resolving-relative-links.html">Relative Link Resolution</a> →</div>
<hr style="clear:both">
<div class="footer"><p class="copyright">Copyright © 2004, 2005, 2006 Mark Pilgrim</p></div>
</div></div>
</body>
</html>
